﻿// This code is released under the BSD license.
namespace PugLib.UI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows.Forms;
    using Filters;

    public partial class FilterBuilder : PugForm
    {
        private readonly SearchFilterGroup _mainState;

        public FilterBuilder()
        {
            InitializeComponent();

            _mainState = new SearchFilterGroup();
        }


        /// <summary>
        /// Default is true.  If true, on calling GetFilters, it will remove duplicate items.
        /// </summary>
        public bool RemoveDuplicatesOnGetFilters
        {
            get { return filterPickers.RemoveDuplicatesOnGetFilters; }
            set { filterPickers.RemoveDuplicatesOnGetFilters = value; }
        }

        #region events

        private void save_Click(object sender, EventArgs e)
        {
            _mainState.Filters.Clear();
            _mainState.Filters.AddRange(filterPickers.Filters);

            _mainState.MatchType = matchAll.Checked ? MatchStyle.All : MatchStyle.Any;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void add_Click(object sender, EventArgs e)
        {
            filterPickers.AddFilter();
        }

        #endregion events

        #region getters and setters

        public IList<SearchFilter> Filters
        {
            get { return _mainState.Filters; }
        }

        public void SetState(SearchFilterGroup settings)
        {
            SetFieldsForAllPickers(settings.Fields);
            SetMatchType(settings.MatchType);
            LoadFilters(settings.Filters);
        }

        public void SetFieldsForAllPickers(ICollection<string> fields)
        {
            _mainState.SetFields(fields);
            filterPickers.SetFieldsForAllPickers(_mainState.Fields);
        }

        public void SetMatchType(MatchStyle matchStyle)
        {
            matchAll.Checked = (matchStyle == MatchStyle.All);
        }

        public void LoadFilters(IList<SearchFilter> filters)
        {
            if (filters == null)
            {
                return;
            }

            if (!filterPickers.HasFields)
            {
                throw new InvalidOperationException(Strings.InvalidFilterLoad);
            }

            // remove default filter
            filterPickers.Clear();

            if (filters.Count == 0)
            {
                // set default of 1
                FilterPicker fp = new FilterPicker();
                fp.SetFields(_mainState.Fields);
                filterPickers.AddFilterPicker(fp);
                return;
            }

            foreach (SearchFilter filter in filters)
            {
                FilterPicker fp = new FilterPicker();
                fp.SetFields(_mainState.Fields);
                fp.SetField(filter.Field);
                fp.SetSearchOperator(filter.SearchOperator);
                fp.SetFilter(filter.SearchText);
                filterPickers.AddFilterPicker(fp);
            }
        }

        #endregion getters and setters

        public void TestShow()
        {
            Show();
            Thread.Sleep(1000);
            Close();
        }

    }
}